import ult.BaseDao;

import java.util.Scanner;

public class book {
    static String a1 = "1719890482", s1 = "shenqizhe";
    public static String[] book1 = new String[]{"西游记", "水浒传", "三国演义", "红楼梦", "假如给我三天光明"};
    public static int[] book = new int[]{50, 60, 40, 45, 20};
    public static int[] bookDZ = new int[]{50, 60, 40, 45, 20};

    public static void main(String[] args) {
        system1();
    }

    public static void system1() {
        TJ();
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("------图书管理系统------");
            System.out.println("请先登入账号");
            System.out.print("输入账号：");
            String a = sc.next();
            System.out.print("输入密码：");
            String s = sc.next();
            if (a1.equals(a) && s1.equals(s)) {
                System.out.println("登入成功！");
                break;
            } else System.out.println("账号或密码错误，请重新登入。");
        }
        a : while (true) {
            System.out.println("1.借书  2.还书  3.退出");
            String s2 = sc.next();
            switch (s2) {
                case "借书" -> borrowBook();
                case "还书" -> returnBook();
                case "退出" -> {
                    System.out.println("退出");
                    break a;
                }
            }
        }
    }

    //还书
    public static void returnBook() {
        Scanner sc = new Scanner(System.in);
        System.out.println("输入你要还的书的名字、id、数量");
        String s1 = sc.next(), s2 = sc.next();
        int a = sc.nextInt();
        huanshu(s2, a, s1);
    }

    //借书
    public static void borrowBook() {
        Scanner sc = new Scanner(System.in);
        System.out.print("图书有： \n001《西游记》" + book[0] + "\n002《水浒传》" + book[1] + "\n003《三国演义》" + book[2] + "\n004《红楼梦》" + book[3] + "\n005《假如给我三天光明》" + book[4] + "\n输入您要借的书的id：");
        String s = sc.next();
        switch (s) {
            case "001" -> panduan(s);
            case "002" -> panduan(s);
            case "003" -> panduan(s);
            case "004" -> panduan(s);
            case "005" -> panduan(s);
            default -> System.out.println("没有此" + s + "id这本书");
        }
    }

    public static void panduan(String s) {
        int a = s.charAt(2) - '0';
        if (book[a] != 0) {
            System.out.println("借阅成功");
            book[a]--;
            String b = "delete from book;";
            BaseDao.executeDML(b);
            TJ();
        } else System.out.println("该书库存不足，借阅失败");
    }

    public static void huanshu(String s, int c, String s1) {
        int a = s.charAt(2) - '0';
        if (book[a] + c <= bookDZ[a] && s1.equals(book1[a - 1])) {
            System.out.println("还书成功");
            book[a] += c;
            String b = "delete from book;";
            BaseDao.executeDML(b);
            TJ();
        } else System.out.println("还书失败。还的不是该id的书。");
    }

    // 添加数据进数据库
    public static void TJ() {
        String b = "delete from book;";
        BaseDao.executeDML(b);
        int a = 0;
        for (int i = 0; i <= book[a]; ++i) {
            String s = "insert into book values(?,?)";
            String a1 = "00" + (a + 1), a2 = book1[a];
            BaseDao.executeDML(s, a1, a2);
            if (i - 1 == book[a] - 2) {
                if (a == 4) break;
                i = -1;
                a++;
            }
        }
    }

}
